let express = require('express');
let db = require('./nodedb');
let router = express.Router();
let bodyparser = require("body-parser")
let urlpar = bodyparser.urlencoded({ extended: false })

// get:req.query    |   post:req.body

// 获取数据
router.get('/list', async function (req, res) {
    let { search, currentPage = 0, pageSize = 10, sort = 'desc', orderby = 'id' } = req.query;
    var sql = `select * from classify`;
    let sqlCount = `select count(*) as total from classify`

    if (search) {
        sql += `   where classify.name like '%${search}%'`
    }

    if (orderby) {
        sql += `  order by ${orderby} ${sort}`;
    }

    /*// 方法一
        const data = await Promise.all([db.dosql(sql)])
        res.send(db.hand(data[0]))
    */
    // 方法二
    let r = await db.dosql(sql)
    let rCount = await db.dosql(sqlCount)
    console.log();
    res.send(db.getData(r, rCount[0].total))
})

// 新增
router.post('/addList', urlpar, async function (req, res) {
    let { name } = req.body;
    let sql = `insert into classify (name) values ('${name}')`;
    let r = await db.dosql(sql)
    res.send(db.addData(r))
})

// 修改
router.post('/updateList', urlpar, async function (req, res) {
    let { id, name } = req.body;
    var sql = `update classify set name='${name}' where id='${id}'`
    let r = await db.dosql(sql)
    console.log(r);
    res.send(db.updataData(r))
})

// 删除
router.get('/deleteList', async function (req, res) {
    let { id } = req.query;
    let sql = `delete from classify where id in (${id})`;
    let r = await db.dosql(sql);
    res.send(db.deleteData(r))
})


module.exports = router;


// let express = require("express")
// let nbd = require("./nodedb")
// let bodyParser = require("body-parser")
// let urlenCoded = bodyParser.urlencoded({ extended: false })
// let router = express.Router()
// // get:req.query    |   post:req.body

// router.get("/", async (req, res) => {
//     let sql = `select * from msbd`
//     let r = await nbd.dosql(sql)
//     res.send(nbd.getData(r))
// })

// // 获取数据
// router.get("/list", async (req, res) => {
//     let { currentPcontent = 0, pcontentSize = 5, order, by, search } = req.query
//     let sql = `select * from msbd`
//     let sqla = `select count(*) as total from msbd as c`
//     let num = currentPcontent * pcontentSize

//     if (search) {
//         sql += ` and m.title like '${search}`
//         sqla += ` and m.title like '${search}`
//     }
//     if (order) {
//         sql += ` order by ${order} ${by}`
//     }
//     sql += ` limit ${num},${pcontentSize}`

//     let r = await nbd.dosql(sql)
//     let rl = await nbd.dosql(sqla)
//     res.send(nbd.hand(r,rl[0].total))
// })

// router.post('/save', urlenCoded, async (req, res) => {//成功
//     let { title } = req.body
//     let sql = `insert into msbd (title) values ('${title}')`
//     let r = await nbd.dosql(sql)
//     res.send(nbd.addData(r))
// })

// router.post('/update', urlenCoded, async (req, res) => {
//     let { id, content } = req.body
//     let sql = `update msbd set content='${content}' where id=${id}`
//     let r = await nbd.dosql(sql)
//     res.send(nbd.updateData(r))
// })

// router.get("/delete", async (req, res) => {
//     let { id } = req.query
//     let sql = `delete from msbd where id='${id}'`
//     let r = await nbd.dosql(sql)
//     res.send(nbd.deleteData(r))
// })

// module.exports = router